package Thread;

import java.util.Random;

public class ThreadDemo16 {
    static Object object = new Object();
    static int sum;
    public static void main(String[] args) {
        int []array = new int[10000000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100);
        }
        Thread t1 = new Thread(()->{
            for (int i = 0; i < array.length; i+=2) {
               synchronized (object){
                   sum+=array[i];
               }
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 1; i < array.length; i+=2) {
                synchronized (object){
                    sum+=array[i];
                }
            }
        });
      long p1 = System.currentTimeMillis();
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long p2 = System.currentTimeMillis();
        System.out.println(sum);
        System.out.println(p2-p1);
    }
}
